﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form打印图纸 : Form
    {
        public Form打印图纸()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string 物料编号;
                string 生产订单号;
                string 生产订单行号;
                string 加工顺序;
                string 工序编号;
                int 生产订单行ID;
                long 派工id;
                string 工票码;
                decimal 派工数量;
                decimal 已报工数量;
                decimal 数量;
                if (!pub.解析条码(this.textBox1.Text.Trim(), out 生产订单号, out 生产订单行号, out 物料编号, out 数量, out 加工顺序, out 工序编号, out 派工id, out 派工数量))
                {
                    MessageBox.Show("您扫描的条码没法解析，请重扫!", "提示", MessageBoxButtons.OK);
                    return;
                }
				//查询打印记录
				string sql打印记录 = "select count(1) from Z_图纸打印记录 where 产品编号 = '"+ 物料编号 + "' and 工票码 = '"+ this.textBox1.Text.Trim() + "'";
				int r = (int)SqlHelper.SqlHelper.ExecuteScalar(sql打印记录);
                if (r > 0)
                {
					DialogResult d = MessageBox.Show("该产品已打印"+r+"次。是否继续打印？","提示",MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
						ThirdExeInterface th = new ThirdExeInterface();
						th.RunThirdExe(ConfigurationManager.AppSettings["printPath"].ToString(), 物料编号);
					}
                }
                else
                {
					ThirdExeInterface th = new ThirdExeInterface();
					th.RunThirdExe(ConfigurationManager.AppSettings["printPath"].ToString(), 物料编号);
				}
				//塞打印记录
				string sql塞打印记录 = "insert into Z_图纸打印记录 (产品编号,工票码,打印时间) values ('"+ 物料编号 + "','"+ this.textBox1.Text.Trim() + "',GETDATE());";
				SqlHelper.SqlHelper.ExecuteNonQuery(sql塞打印记录);
				
            }
        }
        [DllImport("shell32.dll")]
        static extern void print图纸(string path,string 物料编号);
        //private static extern void print图纸(string 物料编号)
        //{
        //    throw new NotImplementedException();
        //}
    }

	public class ThirdExeInterface
	{
		[DllImport("User32.dll", EntryPoint = "SetParent")]
		private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

		[DllImport("user32.dll", EntryPoint = "ShowWindow")]
		private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

		[DllImport("user32.dll", SetLastError = true)]
		private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

		IntPtr intPtr = new IntPtr();
		//检测进程是否存在
		public bool CheckProcessExists(string szProcessName)
		{
			try
			{
				Process[] processes = Process.GetProcessesByName(szProcessName);
				foreach (Process p in processes)
				{
					if (System.IO.Path.Combine(Application.StartupPath, szProcessName) == p.MainModule.FileName)
						return true;
				}
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
			return false;
		}

		//杀死进程
		private void KillProcess(string szProcessName)
		{
			Process[] processes = Process.GetProcessesByName(szProcessName);
			foreach (Process p in processes)
			{
				if (System.IO.Path.Combine(Application.StartupPath, szProcessName) == p.MainModule.FileName)
				{
					p.Kill();
					p.Close();
				}
			}
		}

		//运行
		public void RunThirdExe(string szProcessName,string invCode)
		{
			try
			{
				if (CheckProcessExists(szProcessName))
				{
					KillProcess(szProcessName);
				}

				string fexePath = szProcessName; // 外部exe位置，如 fexePath  = @"DrawingTools.exe";//位于执行目录下

				Process p = new Process();
				p.StartInfo.FileName = fexePath;
				p.StartInfo.UseShellExecute = false;
				p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
				p.StartInfo.Arguments = invCode+" 0";
				p.Start();

				while (p.MainWindowHandle.ToInt32() == 0)
				{
					System.Threading.Thread.Sleep(100);
				}
				//SetParent(p.MainWindowHandle, this.Handle);
				intPtr = p.MainWindowHandle;
				MoveWindow(p.MainWindowHandle, 0, 30, 18,  50, true);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Source + " " + e.Message);
			}
		}

		//隐藏
		public void HideThirdExe()
		{
			ShowWindow(intPtr, (int)ProcessWindowStyle.Normal);//隐藏
		}
	}

}
